package com.smart.primary.sys.service.impl;

import com.smart.primary.common.util.Util;
import com.smart.primary.sys.dao.ResMapper;
import com.smart.primary.sys.service.ResService;
import com.smart.primary.sys.vo.Res;
import com.smart.primary.sys.vo.TreeNode;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class ResServiceImpl implements ResService {
    @Resource
    private ResMapper sysResMapper;

    public ResServiceImpl() {
    }

    public Res queryObject(Res sysRes) throws Exception {
        return (Res)this.sysResMapper.queryObject(sysRes);
    }

    public List<Res> queryList(Res sysRes) throws Exception {
        return this.sysResMapper.queryList(sysRes);
    }

    public List<Res> queryListByPage(Res sysRes) throws Exception {
        return this.sysResMapper.queryListByPage(sysRes);
    }

    public Integer queryCount(Res sysRes) throws Exception {
        Integer ct = this.sysResMapper.queryCount(sysRes);
        return ct == null ? 0 : ct;
    }

    public Integer insert(Res sysRes) throws Exception {
        sysRes.setResId(Util.getUUID());
        sysRes.setCreateTime(new Date());
        sysRes.setUpdateTime(new Date());
        sysRes.setIsLeaf(1);
        return this.sysResMapper.insert(sysRes);
    }

    public Integer update(Res sysRes) throws Exception {
        sysRes.setUpdateTime(new Date());
        return this.sysResMapper.update(sysRes);
    }

    public Integer delete(String resId) throws Exception {
        return this.sysResMapper.delete(resId);
    }

    public List<Object> getSysResTreeList(Res sysRes) throws Exception {
        List<Object> resList = new ArrayList();
        List<Res> sysResList = this.sysResMapper.queryList(sysRes);
        Iterator var4 = sysResList.iterator();

        while(var4.hasNext()) {
            Res res = (Res)var4.next();
            String parentId = res.getParentId();
            TreeNode node = new TreeNode();
            node.setParentId(parentId);
            node.setId(res.getResId());
            node.setName(res.getResName());
            Map<String, String> attrs = new HashMap();
            Integer systemFlag = res.getIsSystem();
            if (systemFlag != null && 1 == systemFlag) {
                attrs.put("sysFlag", "1");
            } else {
                attrs.put("sysFlag", "0");
            }

            Integer leafFlag = res.getIsLeaf();
            if (leafFlag != null && 1 == leafFlag) {
                node.setParent(false);
            } else {
                node.setParent(true);
            }

            node.setAttrs(attrs);
            resList.add(node);
        }

        return resList;
    }

    public List<Res> getMenuList(String userId) throws Exception {
        new ArrayList();
        List<Res> sysResList = this.sysResMapper.getUserMenuList(userId);
        return this.buildListToTree(sysResList);
    }

    public Integer getChildCount(Res sysRes) throws Exception {
        return this.sysResMapper.getChildCount(sysRes);
    }

    private List<Res> buildListToTree(List<Res> dirs) {
        List<Res> roots = this.findRoots(dirs);
        List<Res> notRoots = (List) CollectionUtils.subtract(dirs, roots);
        Iterator var4 = roots.iterator();

        while(var4.hasNext()) {
            Res root = (Res)var4.next();
            root.setSubList(this.findChildren(root, notRoots));
        }

        return roots;
    }

    public List<Res> findRoots(List<Res> allSystemFunctions) {
        List<Res> results = new ArrayList();
        Iterator var3 = allSystemFunctions.iterator();

        while(var3.hasNext()) {
            Res node = (Res)var3.next();
            if ("0".equals(node.getParentId())) {
                results.add(node);
            }
        }

        return results;
    }

    private List<Res> findChildren(Res root, List<Res> allSystemFunctions) {
        List<Res> children = new ArrayList();
        Iterator var4 = allSystemFunctions.iterator();

        while(var4.hasNext()) {
            Res comparedOne = (Res)var4.next();
            if (comparedOne.getParentId().equals(root.getResId())) {
                children.add(comparedOne);
            }
        }

        List<Res> notChildren = (List)CollectionUtils.subtract(allSystemFunctions, children);
        Iterator var9 = children.iterator();

        while(var9.hasNext()) {
            Res child = (Res)var9.next();
            List<Res> tmpChildren = this.findChildren(child, notChildren);
            child.setSubList(tmpChildren);
        }

        return children;
    }
}